﻿using System;
using System.Collections.Generic;
using System.Linq;
using TimeMachine.Engine.Storing;
using TimeMachine.Engine.Vcs;

namespace TimeMachine.Engine.Analytics.Impl
{
	public class ProjectInfoProvider : IProjectInfoProvider
	{
		private readonly ICommitsInfoProvider commitsProvider;
		private readonly IMetricsStorage metricsValues;

		public ProjectInfoProvider(IMetricsStorage metricsValues, ICommitsInfoProvider commitsProvider)
		{
			this.metricsValues = metricsValues;
			this.commitsProvider = commitsProvider;
		}

		public ProjectInfo GetInfo()
		{
			IEnumerable<DetailedCommitInfo> commits = commitsProvider.Get();
			IEnumerable<string> metrics = metricsValues.KnownMetricIds();
			return
				new ProjectInfo
					{
						Authors = commits.Select(c => c.Author).Distinct().ToArray(),
						Metrics = metrics.ToArray(),
						Since = commits.Any() ? commits.First().Date : DateTime.Now,
						Till = commits.Any() ? commits.Last().Date : DateTime.Now,
						TotalDays = commits.Any() ? (commits.Last().Date - commits.First().Date).TotalDays + 1 : 1
					};
		}
	}
}